package cn.tedu.myboot42.controller;

import cn.tedu.myboot42.mapper.UserMapper;
import cn.tedu.myboot42.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/check")
    @ResponseBody
    public int check(String username){
        User u = mapper.selectByUsername(username);
        if(u!=null){
            return 2;
        }
        return 1;
    }

    @RequestMapping("/reg")
    @ResponseBody
    public int reg(User user){
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            return 2;
        }
        mapper.insert(user);
        return 1;
    }

    @RequestMapping("/login")
    @ResponseBody
    public int login(User user){
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            if(u.getPassword().equals(user.getPassword())){
                return 1;
            }
            return 3;
        }
        return 2;
    }
}
